package com.cicc.beergamevirtualsimulationsystem.controller;

import com.cicc.beergamevirtualsimulationsystem.common.constant.RecordConstant;
import com.cicc.beergamevirtualsimulationsystem.model.R;
import com.cicc.beergamevirtualsimulationsystem.model.record.vo.RecordQueryVO;
import com.cicc.beergamevirtualsimulationsystem.service.RecordService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 局内功能
 */
@Validated
@RestController
@RequestMapping("/api/record")
public class RecordController {
    @Resource
    private RecordService recordService;

    /**
     * 记录初始化
     *
     * @param roomId   房间id
     * @param period   周期
     * @param orderNum 客户订单数
     * @return
     */
    @PostMapping("/init")
    public R initRecord(@NotBlank(message = RecordConstant.ROOM_ID_NULL) String roomId,
                        @NotNull(message = RecordConstant.PERIOD_ID_NULL) Integer period,
                        @NotNull(message = RecordConstant.NUM_ERROR) Integer orderNum) {
        recordService.initRecord(roomId, period, orderNum);
        return R.ok();
    }

    @PostMapping("/initTest")
    public R initRecordTest(@NotBlank(message = RecordConstant.ROOM_ID_NULL) String roomId,
                            @NotNull(message = RecordConstant.PERIOD_ID_NULL) Integer period,
                            @NotNull(message = RecordConstant.NUM_ERROR) Integer orderNum) {
        recordService.initRecordTest(roomId, period, orderNum);
        return R.ok();
    }

    /**
     * 查询单条记录
     *
     * @param roomId 房间id
     * @param userId 用户id
     * @return
     */
    @GetMapping("/{roomId}/{userId}/{period}")
    public R<RecordQueryVO> queryRecord(@NotBlank(message = RecordConstant.ROOM_ID_NULL) @PathVariable String roomId,
                                        @NotBlank(message = RecordConstant.USER_ID_NULL) @PathVariable String userId) {
        RecordQueryVO recordQueryVO = recordService.queryRecord(roomId, userId);
        return R.ok(recordQueryVO);
    }

    /**
     * 订货
     *
     * @param recordId 记录id
     * @param orderNum 订货量
     * @return
     */
    @PostMapping("/orderGoods")
    public R<RecordQueryVO> orderGoods(@NotNull(message = RecordConstant.RECORD_ID_NULL) String recordId,
                        @NotNull(message = RecordConstant.ORDER_NUM_NULL)
                        @Min(value = RecordConstant.NUM_MIN, message = RecordConstant.NUM_ERROR) Integer orderNum) {
        RecordQueryVO recordQueryVO = recordService.orderGoods(recordId, orderNum);
        return R.ok(recordQueryVO);
    }

    /**
     * 自定义发货
     *
     * @param recordDetailId 记录明细id
     * @param deliverNum     发货量
     * @return
     */
    @PostMapping("/deliverGoods")
    public R<RecordQueryVO> deliverGoods(@NotNull(message = RecordConstant.RECORD_DETAIL_ID_NULL) String recordDetailId,
                          @NotNull(message = RecordConstant.DELIVER_NUM_NULL)
                          @Min(value = RecordConstant.NUM_MIN, message = RecordConstant.NUM_ERROR) Integer deliverNum) {
        RecordQueryVO recordQueryVO = recordService.deliverGoods(recordDetailId, deliverNum);
        return R.ok(recordQueryVO);
    }

    /**
     * 周期结算
     *
     * @param roomId 房间id
     * @param period 周期
     * @return
     */
    @PostMapping("/settle")
    public R settle(@NotBlank(message = RecordConstant.ROOM_ID_NULL) String roomId,
                    @NotNull(message = RecordConstant.PERIOD_ID_NULL) Integer period) {
        recordService.settle(roomId, period);
        return R.ok();
    }

    /**
     * 一键发货
     *
     * @param recordId 记录id
     * @return
     */
    @PostMapping("/oneClick-deliver")
    public R<RecordQueryVO> oneClickDeliver(@NotNull(message = RecordConstant.RECORD_ID_NULL) String recordId) {
        RecordQueryVO recordQueryVO = recordService.oneClickDeliver(recordId);
        return R.ok(recordQueryVO);
    }

    /**
     * 平均发货
     *
     * @param recordId 记录id
     * @return
     */
    @PostMapping("/average-deliver")
    public R<RecordQueryVO> averageDeliver(@NotNull(message = RecordConstant.RECORD_ID_NULL) String recordId){
        RecordQueryVO recordQueryVO = recordService.averageDeliver(recordId);
        return R.ok(recordQueryVO);
    }

    /**
     * 工厂产能升级
     * @param recordId 记录id
     * @return
     */
    @PostMapping("/upgrade-capacity")
    public R<RecordQueryVO> upgradeCapacity(@NotNull(message = RecordConstant.RECORD_ID_NULL)  String recordId){
        RecordQueryVO recordQueryVO = recordService.upgradeCapacity(recordId);
        return R.ok(recordQueryVO);
    }

    /**
     * 查询主持人的下单记录
     */
    @GetMapping("/queryHostOrder/{roomId}")
    public R<Integer> queryHostOrder(@NotBlank(message = RecordConstant.ROOM_ID_NULL) @PathVariable String roomId) {
        return R.ok(recordService.queryHostOrder(roomId));
    }

}
